package com.example.madman.yuejian.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.madman.yuejian.Bean.CoachBean;
import com.example.madman.yuejian.CoachActivity;
import com.example.madman.yuejian.IP.IP_Config;
import com.example.madman.yuejian.R;

import java.util.List;


public class CoachListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private  List<CoachBean> list;
    private Context context;
    private LayoutInflater inflater;
    private IP_Config IPAddress = new IP_Config();

    public CoachListAdapter(Context context)
    {
        this.inflater = LayoutInflater.from(context);
        this.context=context;
    }

    public void setList(List<CoachBean> l){
        list=l;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.fragment_coachlist,parent,false);
        RecyclerView.ViewHolder vh = new CoachListAdapter.ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) {
        CoachBean bean = list.get(position);
        if(bean==null) return;
        final ViewHolder viewHolder= (ViewHolder) holder;
        viewHolder.name.setText(bean.getCoachname());
        viewHolder.icoach.setImageBitmap(BitmapFactory.decodeResource(
                context.getResources(), R.drawable.club));
        viewHolder.level.setText(bean.getCoachlevel());
        Glide.with(context)
                .load(IPAddress.IP+"/yuejian/public/static/assets/img/"+bean.getPic())
                . placeholder(R.drawable.club)
                .into(viewHolder.icoach);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getLayoutPosition();

                Intent intent;
                intent = new Intent(context,CoachActivity.class);
                intent.putExtra("coachid",list.get(position).getCoachid());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name,level;
        ImageView icoach;

        public ViewHolder(View itemview) {
            super(itemview);

            name = (TextView) itemview.findViewById(R.id.coachlist_name);
            level = (TextView) itemview.findViewById(R.id.coachlist_level);
            icoach = (ImageView) itemview.findViewById(R.id.coachlist_image);
        }


    }
}
